package com.cmc.cloud.cmclink.doc.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.vo.limitrulevo.LimitRuleCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.limitrulevo.LimitRulePageReqVO;
import com.cmc.cloud.cmclink.doc.vo.limitrulevo.LimitRuleRespVO;
import com.cmc.cloud.cmclink.doc.vo.limitrulevo.LimitRuleUpdateReqVO;
import com.cmc.cloud.cmclink.doc.vo.rulematchitemcfg.RuleMatchItemCfgRespVO;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.pojo.CommonResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.validation.constraints.NotNull;
import java.util.*;


import com.cmes.framework.common.pojo.PageResult;

import com.cmc.cloud.cmclink.doc.entity.LimitRuleDO;
import com.cmc.cloud.cmclink.doc.service.LimitRuleService;


/**
 * {table.classComment}前端接口Controller
 * 
 * @author codegen
 */
@Tag(name = "管理后台 - 禁接规则")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL+"/limitRule")
public class LimitRuleController {

    @Resource
    private LimitRuleService limitRuleService;

    @Operation(summary = "创建禁接规则")
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermission('doc:maintainBCRDCR:add')")
    public CommonResult<Void> createLimitRule(@Valid @RequestBody LimitRuleCreateReqVO createReqVO) {
        limitRuleService.createLimitRule(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新禁接规则")
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermission({'doc:maintainBCRDCR:edit','doc:maintainBCRDCR:enable'})")
    public CommonResult<Void> updateLimitRule(@Valid @RequestBody LimitRuleUpdateReqVO updateReqVO) {
        limitRuleService.updateLimitRule(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除禁接规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PostMapping("/delete/{id}")
    @PreAuthorize("@ss.hasPermission('doc:maintainBCRDCR:delete')")
    public CommonResult<Void> deleteLimitRule(@PathVariable("id") Long id) {
        limitRuleService.deleteLimitRule(id);
        return CommonResult.success();
    }

    @Operation(summary = "获得禁接规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @GetMapping("/getById/{id}")
    public CommonResult<LimitRuleRespVO> getLimitRule(@PathVariable("id") Long id) {
        LimitRuleDO limitRule = limitRuleService.getLimitRule(id);
        return CommonResult.success(BeanUtil.copyProperties(limitRule, LimitRuleRespVO.class));
    }

    @Operation(summary = "获得禁接规则列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @GetMapping("/listAll")
    public CommonResult<List<LimitRuleRespVO>> getLimitRuleList(@RequestParam("ids") Collection<Long> ids) {
        List<LimitRuleDO> list = limitRuleService.getLimitRuleList(ids);
        return CommonResult.success(BeanUtil.copyToList(list,LimitRuleRespVO.class));
    }

    @Operation(summary = "获得禁接规则分页")
    @PostMapping("/getPage")
    @UseResponsePropertyAdvice
    @PreAuthorize("@ss.hasPermission('doc:maintainBCRDCR:search')")
    public CommonResult<PageResult<LimitRuleRespVO>> getLimitRulePage(@Valid @RequestBody LimitRulePageReqVO pageVO) {
        PageResult<LimitRuleDO> pageResult = limitRuleService.getLimitRulePage(pageVO);
        List<LimitRuleRespVO> results = BeanUtil.copyToList(pageResult.getList(),LimitRuleRespVO.class);
        return CommonResult.success(new PageResult<>(results,pageResult.getTotal()));
    }
    @Operation(summary = "BCR/DCR规则匹配项配置接口")
    @Parameter(name = "usePeriod", description = "适用阶段", required = true, example = "DCR/BCR")
    @GetMapping("/matchItemConfiguration/{usePeriod}")
    public CommonResult<List<RuleMatchItemCfgRespVO>> qryMatchItemConfiguration(@PathVariable("usePeriod") @NotNull String usePeriod) {
        return CommonResult.success(limitRuleService.getMatchItemConfiguration(usePeriod));
    }


}
